<?php

/**
 * This file is part of the Pagizer package.
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 *
 * @copyright   Copyright (c) 2010 Advisa (http://www.advisa.fr)
 * @author      Pagizer Core Team <team@pagizer.org>
 * @package     Pagizer
 * @subpackage  backoffice
 */

class m_backoffice_actions_block_GenericPictureSelectAction extends m_backoffice_extends_FormModal
{
	public function execute()
	{
        $mode = $this->getRequestParameter("mode", null);

        if(is_null($mode))
        {
            $this->getContext()->setContext("moduleUse", $this->getRequestParameter("module"));
        }
        $moduleUse = $this->getContext("moduleUse");
		$docId = $mode == "1" ? $this->getRequestParameter("docId") : $this->getBlockParameter("docId");
        $formName = $this->getBlockParameter("formName", null);
        $lang = $this->getContextLang();

        if($this->getDocumentProvider()->checkDocument($docId))
        {
            $media = $this->getDocumentProvider()->getByModelAndUniqueId("modules_medias/media", $docId, $lang);
        }

        if(isset($media) && $media->isImage())
        {

            $pages = $this->getDocumentProvider()->loadDocuments("modules_website/page")
                            ->whereIsEqual("websiteId", $this->getContext("currentWebsite")->getUniqueId())->retrieveDocuments($lang);

            $values = array();

            $check = array();
            
            foreach($pages as $page)
            {
                $blockParams = $page->getAllBlocks();
                foreach($blockParams as $blockParam)
                {
                    $sizeKey = array();
                    foreach($blockParam as $key => $param)
                    {
                        if(preg_match('#^picWidth(.*)$#', $key, $matches))
                        {
                            if(isset($blockParam["picHeight".$matches[1]]))
                            {
                                $sizeKey[] = array('width' => $key, 'height' => "picHeight".$matches[1]);
                            }
                        }
                    }

                    if(!empty($sizeKey))
                    {                                                
                        foreach($sizeKey as $key)
                        {
                            if(isset($blockParam["module"]) && $blockParam["module"] == $moduleUse)
                            {
                                $mediaUrl = $media->getImageUrlByMaxSize($blockParam[$key['width']], $blockParam[$key['height']]);
                                if(!is_null($mediaUrl))
                                {
                                    $mediaUrl .= "?" . rand(0,50);
                                }

                                $mediaWidth = $blockParam[$key['width']];
                                $mediaHeight = $blockParam[$key['height']];

                                if($mediaWidth != '' && $mediaHeight != '' && !isset($check[$mediaWidth][$mediaHeight]))
                                {
                                    $values[] = array("blockLabel" => $blockParam["blockLabel"], "width" => $mediaWidth, "height" => $mediaHeight, "media" => $mediaUrl);
                                    // check if size is not already in list
                                    $check[$mediaWidth][$mediaHeight] = true;
                                }
                            }
                        }
                    }
                }
            }

            $this->addToModel('media', $media);
            $this->addToModel('sizes',$values);
        }

        $this->addToModel('mode', $mode);
        $this->addToModel('formName',  $formName);
		$this->setView('success');
	}
}